﻿namespace AutoRevit.Entry.Command;

internal class InterceptorExtendHandler : IExtendHandler
{
    HandlerResult IExtendHandler.Execute(IRevitData revitData, object extend)
    {
        if (extend is IInterceptorExtend interceptorExtend)
        {
            if (interceptorExtend.CanInterceptorExecute(revitData))
            {
                var r = HandlerInterceptorResult(revitData, interceptorExtend.Interceptors);

                if (!r.CanContinue)
                {
                    return r;
                }
            }
        }

        return new HandlerResult(true);
    }

    /// <summary>
    /// 执行处理拦截器并返回
    /// </summary>
    /// <param name="inputRevitData"></param>
    /// <param name="externalCommandHandlerInterceptors"></param>
    /// <returns></returns>
    protected virtual HandlerResult HandlerInterceptorResult(IRevitData inputRevitData, IEnumerable<IExternalCommandInterceptor> externalCommandHandlerInterceptors)
    {
        foreach (IExternalCommandInterceptor echi in externalCommandHandlerInterceptors)
        {
            var result = echi.Handle(inputRevitData);

            if (!result.CanContinue)
            {
                return result;
            }
        }

        return new HandlerResult(true);
    }
}
